package com.bupt.godxian.charging.util;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

@Component
public class Timer {
    private final LocalDateTime startTime;
    private final int speed = 10;
    private final long shift = 3600 * 13;

    public Timer() {
        this.startTime = LocalDateTime.now();
    }

    public long getCurTimestamp() {
        LocalDateTime currentTime = LocalDateTime.now();//ChronoUnit.SECONDS将时间设置为秒
    //    return (long) (currentTime.toEpochSecond(null) - this.startTime.toEpochSecond(null)) * speed + startTime.toEpochSecond(null) + shift;
//                (currentTime.until(startTime, ChronoUnit.SECONDS) * speed) + startTime.toEpochSecond() + shift);
   // }
        ZonedDateTime currentDateTime = currentTime.atZone(ZoneId.systemDefault());
        ZonedDateTime startTimeInZone = this.startTime.atZone(ZoneId.of("Asia/Shanghai"));

        return (currentDateTime.toEpochSecond() - startTimeInZone.toEpochSecond()) * speed + startTimeInZone.toEpochSecond() + shift;

    }

    public String getCurFormatTime() {
        LocalDateTime now = LocalDateTime.now();
        long timestamp = (now.toEpochSecond(null) - this.startTime.toEpochSecond(null)) * speed + startTime.toEpochSecond(null) + shift;
        LocalDateTime formattedTime = LocalDateTime.ofEpochSecond(timestamp, 0, null);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return formattedTime.format(formatter);

//        LocalDateTime currentTime = LocalDateTime.now();
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        return currentTime.plusSeconds((long) ((currentTime.toEpochSecond(null) - startTime.toEpochSecond(null)) * speed + startTime.toEpochSecond(null) + shift;
    }
}
